home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWStream / Sources / SLStrmRW.cpp < prev   
Encoding:
Text File  |  1996-04-25  |  15.6 KB  |  562 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLStrmRW.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef SLSTRMRW_H
  13. #include "SLStrmRW.h"
  14. #endif
  15.  
  16. #ifndef FWSTRMRW_H
  17. #include "FWStrmRW.h"
  18. #endif
  19.  
  20. #ifndef FWPRIDEB_H
  21. #include "FWPriDeb.h"
  22. #endif
  23.  
  24. #ifndef FWEXCLIB_H
  25. #include "FWExcLib.h"
  26. #endif
  27.  
  28. #ifndef FWODFEXC_H
  29. #include "FWODFExc.h"
  30. #endif
  31.  
  32. #include "SLASinks.xh"
  33. #include "SLObjReg.xh"
  34.  
  35. #ifdef FW_BUILD_MAC
  36. #pragma segment FWStream
  37. #endif
  38.  
  39. #if defined(__MWERKS__) && GENERATING68K
  40. // A hack to work around a bug
  41. #pragma import list somNewObjectInstance
  42. #endif
  43.  
  44.  
  45. //----------------------------------------------------------------------------------------
  46. // FW_NATIVE_BIGENDIAN, FW_NATIVE_LITTLEENDIAN
  47. //    One and only one of these macros is true to define the native format
  48. //    for storing integral values
  49. //----------------------------------------------------------------------------------------
  50. #ifdef FW_BUILD_MAC
  51. #define    FW_NATIVE_BIGENDIAN            1
  52. #define    FW_NATIVE_LITTLEENDIAN        0
  53. #endif
  54.  
  55. #ifdef FW_BUILD_WIN
  56. #define    FW_NATIVE_BIGENDIAN            0
  57. #define    FW_NATIVE_LITTLEENDIAN        1
  58. #endif
  59.  
  60. #if !FW_NATIVE_BIGENDIAN && !FW_NATIVE_LITTLEENDIAN
  61. #error "Failed to define an endianness"
  62. #endif
  63.  
  64. //----------------------------------------------------------------------------------------
  65. // swapShort
  66. //----------------------------------------------------------------------------------------
  67.  
  68. static short swapShort(short aShort)
  69. {
  70.     union
  71.     {
  72.         short temp;
  73.         char s[2];
  74.     };
  75.     temp = aShort;
  76.     char c = s[0];
  77.     s[0] = s[1];
  78.     s[1] = c;
  79.     return temp;
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. // swapLong
  84. //----------------------------------------------------------------------------------------
  85.  
  86. static long swapLong(long aLong)
  87. {
  88.     union
  89.     {
  90.         long temp;
  91.         char s[4];
  92.     };
  93.     temp = aLong;
  94.     char c = s[0];
  95.     s[0] = s[3];
  96.     s[3] = c;
  97.     c = s[1];
  98.     s[1] = s[2];
  99.     s[2] = c;
  100.     return temp;
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // readableIdentity
  105. //----------------------------------------------------------------------------------------
  106.  
  107. static void readableIdentity(FW_HReadableStream stream,
  108.                              void* destination,
  109.                              long count,
  110.                              long itemSize)
  111. {
  112.     stream->fSink->Read(&stream->fev, destination, count*itemSize);
  113. }
  114.  
  115. //----------------------------------------------------------------------------------------
  116. // writableIdentity
  117. //----------------------------------------------------------------------------------------
  118.  
  119. static void writableIdentity(FW_HWritableStream stream,
  120.                              const void* source,
  121.                              long count,
  122.                              long itemSize)
  123. {
  124.     stream->fSink->Write(&stream->fev, (void*)source, count*itemSize);
  125. }
  126.  
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // readReturnError
  130. //----------------------------------------------------------------------------------------
  131.  
  132. static FW_PlatformError readReturnError(FW_HReadableStream stream,
  133.                                         void* destination,
  134.                                         long count,
  135.                                         long itemSize)
  136. {
  137.     FW_RETURN_ERR_TRY
  138.     {
  139.         readableIdentity(stream, destination, count, itemSize);
  140.     }
  141.     FW_RETURN_ERR_CATCH
  142. }
  143.  
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // writeReturnError
  147. //----------------------------------------------------------------------------------------
  148.  
  149. static FW_PlatformError writeReturnError(FW_HWritableStream stream,
  150.                                          const void* source,
  151.                                          long count,
  152.                                          long itemSize)
  153. {
  154.     FW_RETURN_ERR_TRY
  155.     {
  156.         writableIdentity(stream, source, count, itemSize);
  157.     }
  158.     FW_RETURN_ERR_CATCH
  159. }
  160.  
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // FW_PrivReadableStream_Construct
  164. //----------------------------------------------------------------------------------------
  165.  
  166. FW_PlatformError    SL_API    
  167. FW_PrivReadableStream_Construct(FW_HReadableStream stream, 
  168.                                 FW_OSink* sink,
  169.                                 FW_OObjectRegistry* objectRegistry,
  170.                                 int numberFormat)
  171. {
  172.     FW_RETURN_ERR_TRY
  173.     {
  174.         // Make safe for destruction
  175.         stream->fArchiveCreatedObjectRegistry = (objectRegistry == 0);
  176.  
  177.         stream->fSink = sink;
  178.         stream->fObjectRegistry = objectRegistry;
  179.         stream->fReserved = 0;
  180.         FW_PrimitiveSetMemory(&stream->fev, sizeof(stream->fev), 0);
  181.  
  182.         stream->fNumberFormat = numberFormat;
  183. #if FW_NATIVE_BIGENDIAN
  184.         if (stream->fNumberFormat == FW_kStream_BigEndian)
  185.             stream->fNumberFormat = FW_kStream_NativeEndian;
  186. #endif
  187. #if FW_NATIVE_LITTLEENDIAN
  188.         if (stream->fNumberFormat == FW_kStream_LittleEndian)
  189.             stream->fNumberFormat = FW_kStream_NativeEndian;
  190. #endif
  191.  
  192.         if (stream->fArchiveCreatedObjectRegistry)
  193.             stream->fObjectRegistry = new FW_OBasicObjectRegistry;
  194.     } 
  195.     FW_RETURN_ERR_CATCH
  196. }
  197.  
  198.  
  199. //----------------------------------------------------------------------------------------
  200. // FW_PrivReadableStream_CopyConstruct
  201. //----------------------------------------------------------------------------------------
  202.  
  203. FW_PlatformError    SL_API    
  204. FW_PrivReadableStream_CopyConstruct(FW_HReadableStream stream, 
  205.                                     FW_HReadableStream original)
  206. {    
  207.     stream->fSink                         = original->fSink;
  208.     stream->fObjectRegistry               = original->fObjectRegistry;
  209.     stream->fev                              = original->fev;
  210.     stream->fNumberFormat                  = original->fNumberFormat;
  211.     stream->fArchiveCreatedObjectRegistry = FALSE;
  212.     return FW_xNoError;
  213. }
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // FW_PrivReadableStream_Destruct
  217. //----------------------------------------------------------------------------------------
  218.  
  219. FW_PlatformError    SL_API    
  220. FW_PrivReadableStream_Destruct(FW_HReadableStream stream)
  221. {
  222.     FW_RETURN_ERR_TRY
  223.     {
  224.         if (stream->fArchiveCreatedObjectRegistry)
  225.             delete stream->fObjectRegistry;
  226.     } 
  227.     FW_RETURN_ERR_CATCH
  228. }
  229.  
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // FW_PrivReadableStream_ReadBytes
  233. //----------------------------------------------------------------------------------------
  234.  
  235. FW_PlatformError    SL_API    
  236. FW_PrivReadableStream_ReadBytes(FW_HReadableStream stream, 
  237.                                 void* buffer, 
  238.                                 long count)
  239. {
  240.     return readReturnError(stream, buffer, count, 1);
  241. }
  242.  
  243.  
  244. //----------------------------------------------------------------------------------------
  245. // FW_PrivReadableStream_ReadChars
  246. //----------------------------------------------------------------------------------------
  247.  
  248. FW_PlatformError    SL_API    
  249. FW_PrivReadableStream_ReadChars(FW_HReadableStream stream, 
  250.                                 char* buffer, 
  251.                                 long count)
  252. {
  253.     return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  254. }
  255.  
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // FW_PrivReadableStream_ReadNullTerminatedString
  259. //----------------------------------------------------------------------------------------
  260.  
  261. FW_PlatformError    SL_API    
  262. FW_PrivReadableStream_ReadNullTerminatedString(FW_HReadableStream stream, 
  263.                                                char* nullTerminatedString)
  264. {
  265.     Environment* const ev = &stream->fev;
  266.     FW_OSink* const sink = stream->fSink;
  267.  
  268.     FW_RETURN_ERR_TRY
  269.     {
  270.         long availableReadBytes;
  271.         FW_Boolean foundTerminator = FALSE;
  272.     
  273.         availableReadBytes = sink->GetReadableBytes(ev);
  274.         while (!foundTerminator && availableReadBytes > 0)
  275.         {
  276.             sink->Read(ev, nullTerminatedString, 1);
  277.             --availableReadBytes;
  278.             foundTerminator = (*nullTerminatedString++ == '\0');
  279.         }
  280.  
  281.         if (!foundTerminator)
  282.             FW_Failure(FW_xReadableStream);
  283.     }
  284.     FW_RETURN_ERR_CATCH
  285. }
  286.  
  287.  
  288. //----------------------------------------------------------------------------------------
  289. // FW_PrivReadableStream_ReadShorts
  290. //----------------------------------------------------------------------------------------
  291.  
  292. FW_PlatformError    SL_API    
  293. FW_PrivReadableStream_ReadShorts(FW_HReadableStream stream, 
  294.                                  short* buffer, 
  295.                                  long count)
  296. {
  297.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  298.     {
  299.         return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  300.     }
  301.     else
  302.     {
  303.         FW_PlatformError anError = readReturnError(stream, buffer, count, sizeof(buffer[0]));
  304.         
  305.         if (anError == FW_xNoError)
  306.         {
  307.             for (; count > 0; --count, ++buffer)
  308.                 *buffer = swapShort(*buffer);
  309.         }
  310.         
  311.         return anError;
  312.     }
  313. }
  314.  
  315.  
  316. //----------------------------------------------------------------------------------------
  317. // FW_PrivReadableStream_ReadLongs
  318. //----------------------------------------------------------------------------------------
  319.  
  320. FW_PlatformError    SL_API    
  321. FW_PrivReadableStream_ReadLongs(FW_HReadableStream stream, 
  322.                                 long* buffer, 
  323.                                 long count)
  324. {
  325.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  326.     {
  327.         return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  328.     }
  329.     else
  330.     {
  331.         FW_PlatformError anError = readReturnError(stream, buffer, count, sizeof(buffer[0]));
  332.         
  333.         if (anError == FW_xNoError)
  334.         {
  335.             for (; count > 0; --count, ++buffer)
  336.                 *buffer = swapLong(*buffer);
  337.         }
  338.         
  339.         return anError;
  340.     }
  341. }
  342.  
  343.  
  344. //----------------------------------------------------------------------------------------
  345. // FW_PrivReadableStream_ReadFloats
  346. //----------------------------------------------------------------------------------------
  347.  
  348. FW_PlatformError    SL_API    
  349. FW_PrivReadableStream_ReadFloats(FW_HReadableStream stream, 
  350.                                  float* buffer, 
  351.                                  long count)
  352. {
  353.     return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  354. }
  355.  
  356.  
  357. //----------------------------------------------------------------------------------------
  358. // FW_PrivReadableStream_ReadDoubles
  359. //----------------------------------------------------------------------------------------
  360.  
  361. FW_PlatformError    SL_API    
  362. FW_PrivReadableStream_ReadDoubles(FW_HReadableStream stream, 
  363.                                   double* buffer, 
  364.                                   long count)
  365. {
  366.     return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  367. }
  368.  
  369.  
  370. //----------------------------------------------------------------------------------------
  371. // FW_PrivWritableStream_Construct
  372. //----------------------------------------------------------------------------------------
  373.  
  374. FW_PlatformError    SL_API    
  375. FW_PrivWritableStream_Construct(FW_HWritableStream stream, 
  376.                                 FW_OSink* sink,
  377.                                 FW_OObjectRegistry* objectRegistry,
  378.                                 int numberFormat)
  379. {
  380.  
  381.     FW_RETURN_ERR_TRY
  382.     {
  383.         // Make safe for destruction
  384.         stream->fArchiveCreatedObjectRegistry = (objectRegistry == 0);
  385.  
  386.         stream->fSink = sink;
  387.         stream->fObjectRegistry = objectRegistry;
  388.         stream->fReserved = 0;
  389.         FW_PrimitiveSetMemory(&stream->fev, sizeof(stream->fev), 0);
  390.  
  391.         stream->fNumberFormat = numberFormat;
  392. #if FW_NATIVE_BIGENDIAN
  393.         if (stream->fNumberFormat == FW_kStream_BigEndian)
  394.             stream->fNumberFormat = FW_kStream_NativeEndian;
  395. #endif
  396. #if FW_NATIVE_LITTLEENDIAN
  397.         if (stream->fNumberFormat == FW_kStream_LittleEndian)
  398.             stream->fNumberFormat = FW_kStream_NativeEndian;
  399. #endif
  400.  
  401.         if (stream->fArchiveCreatedObjectRegistry)
  402.             stream->fObjectRegistry = new FW_OBasicObjectRegistry;
  403.     }
  404.     FW_RETURN_ERR_CATCH
  405. }
  406.  
  407.  
  408. //----------------------------------------------------------------------------------------
  409. // FW_PrivWritableStream_CopyConstruct
  410. //----------------------------------------------------------------------------------------
  411.  
  412. FW_PlatformError    SL_API    
  413. FW_PrivWritableStream_CopyConstruct(FW_HWritableStream stream, 
  414.                                     FW_HWritableStream original)
  415. {
  416.     stream->fSink                         = original->fSink;
  417.     stream->fObjectRegistry               = original->fObjectRegistry;
  418.     stream->fev                              = original->fev;
  419.     stream->fNumberFormat                  = original->fNumberFormat;
  420.     stream->fArchiveCreatedObjectRegistry = FALSE;
  421.     return FW_xNoError;
  422. }
  423.  
  424. //----------------------------------------------------------------------------------------
  425. // FW_PrivWritableStream_Destruct
  426. //----------------------------------------------------------------------------------------
  427.  
  428. FW_PlatformError    SL_API    
  429. FW_PrivWritableStream_Destruct(FW_HWritableStream stream)
  430. {
  431.     FW_RETURN_ERR_TRY
  432.     {
  433.         if (stream->fArchiveCreatedObjectRegistry)
  434.             delete stream->fObjectRegistry;
  435.     } 
  436.     FW_RETURN_ERR_CATCH
  437. }
  438.  
  439.  
  440. //----------------------------------------------------------------------------------------
  441. // FW_PrivWritableStream_WriteBytes
  442. //----------------------------------------------------------------------------------------
  443.  
  444. FW_PlatformError    SL_API    
  445. FW_PrivWritableStream_WriteBytes(FW_HWritableStream stream, 
  446.                                  const void* buffer, 
  447.                                  long count)
  448. {
  449.     return writeReturnError(stream, buffer, count, 1);
  450. }
  451.  
  452.  
  453. //----------------------------------------------------------------------------------------
  454. // FW_PrivWritableStream_WriteChars
  455. //----------------------------------------------------------------------------------------
  456.  
  457. FW_PlatformError    SL_API    
  458. FW_PrivWritableStream_WriteChars(FW_HWritableStream stream, 
  459.                                  const char* buffer, 
  460.                                  long count)
  461. {
  462.     return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  463. }
  464.  
  465.  
  466. //----------------------------------------------------------------------------------------
  467. // FW_PrivWritableStream_WriteNullTerminatedString
  468. //----------------------------------------------------------------------------------------
  469.  
  470. FW_PlatformError    SL_API    
  471. FW_PrivWritableStream_WriteNullTerminatedString(FW_HWritableStream stream, 
  472.                                                 const char* nullTerminatedString)
  473. {
  474.     return writeReturnError(stream, 
  475.                             nullTerminatedString, 
  476.                             FW_PrimitiveStringLength(nullTerminatedString) + 1, 
  477.                             sizeof(nullTerminatedString[0]));
  478. }
  479.  
  480. //----------------------------------------------------------------------------------------
  481. // FW_PrivWritableStream_WriteShorts
  482. //----------------------------------------------------------------------------------------
  483.  
  484. FW_PlatformError    SL_API    
  485. FW_PrivWritableStream_WriteShorts(FW_HWritableStream stream, 
  486.                                   const short* buffer, 
  487.                                   long count)
  488. {
  489.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  490.     {
  491.         return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  492.     }
  493.     else
  494.     {
  495.         FW_RETURN_ERR_TRY
  496.         {
  497.             for (; count > 0; --count, ++buffer)
  498.             {
  499.                 short temp = swapShort(*buffer);
  500.                 writableIdentity(stream, &temp, 1, sizeof(temp));
  501.             }
  502.         }
  503.         FW_RETURN_ERR_CATCH
  504.     }
  505. }
  506.  
  507.  
  508. //----------------------------------------------------------------------------------------
  509. // FW_PrivWritableStream_WriteLongs
  510. //----------------------------------------------------------------------------------------
  511.  
  512. FW_PlatformError    SL_API    
  513. FW_PrivWritableStream_WriteLongs(FW_HWritableStream stream, 
  514.                                  const long* buffer, 
  515.                                  long count)
  516. {
  517.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  518.     {
  519.         return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  520.     }
  521.     else
  522.     {
  523.         FW_RETURN_ERR_TRY
  524.         {
  525.             for (; count > 0; --count, ++buffer)
  526.             {
  527.                 long temp = swapLong(*buffer);
  528.                 writableIdentity(stream, &temp, 1, sizeof(temp));
  529.             }
  530.         }
  531.         FW_RETURN_ERR_CATCH
  532.     }
  533. }
  534.  
  535.  
  536. //----------------------------------------------------------------------------------------
  537. // FW_PrivWritableStream_WriteFloats
  538. //----------------------------------------------------------------------------------------
  539.  
  540. FW_PlatformError    SL_API    
  541. FW_PrivWritableStream_WriteFloats(FW_HWritableStream stream, 
  542.                                   const float* buffer, 
  543.                                   long count)
  544. {
  545.     return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  546. }
  547.  
  548.  
  549. //----------------------------------------------------------------------------------------
  550. // FW_PrivWritableStream_WriteDoubles
  551. //----------------------------------------------------------------------------------------
  552.  
  553. FW_PlatformError    SL_API    
  554. FW_PrivWritableStream_WriteDoubles(FW_HWritableStream stream, 
  555.                                    const double* buffer, 
  556.                                    long count)
  557. {
  558.     return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  559. }
  560.  
  561.  
  562.